package com.xs.project.service.impl;

import com.xs.project.common.Result;
import com.xs.project.entity.XsSubCourse;
import com.xs.project.mapper.CourseManagerMapper;
import com.xs.project.pojo.vo.XcCourseReq;
import com.xs.project.pojo.vo.XsCourseSubReq;
import com.xs.project.pojo.vo.resp.CourseSubResp;
import com.xs.project.pojo.vo.resp.StuCompanyResp;
import com.xs.project.pojo.vo.resp.XcCourseResp;
import com.xs.project.service.ICourseManagerService;
import com.xs.project.service.XsSubCourseService;
import com.xs.project.utils.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CourseManagerServiceImpl implements ICourseManagerService {

    @Resource
    private CourseManagerMapper courseManagerMapper;

    @Resource
    private XsSubCourseService xsSubCourseService;
    @Override
    public Result<List<XcCourseResp>> selectCourseList(XcCourseReq request) {
        List<XcCourseResp> list = courseManagerMapper.selectCourseList(request);
        long skipSize = (long) request.getPageSize() * (request.getPageNum() - 1); // 计算需要跳过的元素数量

        List<XcCourseResp> pageRes = list.stream()
                .skip(skipSize)
                .limit(request.getPageSize())
                .collect(Collectors.toList());
        for (XcCourseResp pageRe : pageRes) {
            Integer totalSub = courseManagerMapper.selectSubTotal(pageRe.getId());
            pageRe.setCourseSubTotal(totalSub);
            if ("1".equals(pageRe.getCourseType())){
                pageRe.setCourseTypeStr("文学类");
            }else{
                pageRe.setCourseTypeStr("科技类");
            }
            pageRe.setCourseTimeStr(DateUtil.getDateStr(pageRe.getCourseTime()));
            pageRe.setCreateTimeStr(DateUtil.getDateStr(pageRe.getCreateTime()));
        }
        return Result.success(pageRes,list.size());
    }

    @Override
    public Boolean updateCourseList(XcCourseResp request) {
        courseManagerMapper.updateCourseList(request);
        return true;
    }

    @Override
    public Boolean insertCourseList(XcCourseResp request) {
        courseManagerMapper.insertCourseList(request);
        Integer courseSubTotal = request.getCourseSubTotal();
        if (courseSubTotal != null){
            createCourseSub(request);
        }
        return true;
    }

    @Override
    public Result<List<CourseSubResp>> selectCourseSubList(XsCourseSubReq request) {
        List<CourseSubResp> list = courseManagerMapper.selectCourseSubList(request);
        long skipSize = (long) request.getPageSize() * (request.getPageNum() - 1); // 计算需要跳过的元素数量

        List<CourseSubResp> pageRes = list.stream()
                .skip(skipSize)
                .limit(request.getPageSize())
                .collect(Collectors.toList());
        for (CourseSubResp pageRe : pageRes) {
            if (pageRe.getCourseSubTime() == null){
                continue;
            }
            pageRe.setCourseSubTimeStr(DateUtil.getDateStr(pageRe.getCourseSubTime()));
        }
        return Result.success(pageRes,list.size());
    }

    @Override
    public Boolean updateCourseSubList(CourseSubResp request) {
        request.setCourseSubTime(DateUtil.getDate(request.getCourseSubTimeStr()));
        courseManagerMapper.updateCourseSubList(request);
        return true;
    }

    @Override
    public Boolean insertCourseSubList(CourseSubResp request) {
        courseManagerMapper.insertCourseSubList(request);

        return true;
    }

    @Override
    public Boolean deleteCourseSubList(Integer id) {
        courseManagerMapper.deleteCourseSubList(id);
        return true;
    }

    private void createCourseSub(XcCourseResp request) {
        for (int i = 0; i < request.getCourseSubTotal(); i++) {
            XsSubCourse xsSubCourse = new XsSubCourse();
            xsSubCourse.setCourseId(request.getId());
            xsSubCourse.setCourseSubName("第"+(i+1)+"节");
            xsSubCourseService.insertXsSubCourse(xsSubCourse);
        }

    }
}
